import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/Home/index.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('../views/NotFount.vue'),
    },
  ],
})
router.beforeEach((to, from, next) => {
  let isLogin = localStorage.getItem('isLogin')
  if (isLogin) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      ElMessage({
        message: '请先登录',
        type: 'error',
        showClose: true,
        duration: 2000,
      })
      next('/login')
    }
  }
})
export default router
